/* ========================================================================== */
/*   ProcessHitController.cpp    			                                  */
/*   (c) 2012 Team 7 Hogeschool Utrecht ICT TI                                */
/*		Lars Stolwijk, Gerben Boot, Guido van Hienen, Marcel Boelen			  */
/*                                                                            */
/*   When a player gets hit the ProcessHit gets all information and sends	  */ 
/*		it to the log.													      */
/* ========================================================================== */
#include "ProcessHitController.h"

void ProcessHitController::main()
{
	char weaponVar = 0;
	char playerVar = 0;
	char healthVar = 0;
	short timeVar = 0;
	
	CurrentState = WAITING;

	for(;;)
	{
		if(CurrentState == LOGGING)
		{
			//std::cout << "PHC->LOGGING1/2\n";
			weaponVar = IRResultPool.read().weapon;
			playerVar = IRResultPool.read().player;
			theHealth->DecreaseAndGetHealth();

			RTOS::event e = wait( HealthPlacedFlag );
			theSoundController->PlayHitSound();
			healthVar = theHealth->ReadHealth();
			GameTimePoolMutex.wait();
			timeVar = GameTimePool.read();
			char min = timeVar/60;
			char sec = timeVar%60;
			GameTimePoolMutex.signal();
			std::cout << "HIT - Player: " << (short)playerVar << " - Weapon: " << (short)weaponVar << " - Health: " << (short)healthVar << " - Time: "  << (short)min << ":" << (short)sec << "\n";
			theLog->WriteLog(playerVar,weaponVar, healthVar, timeVar);
			
			CurrentState = WAITING;
			//std::cout << "PHC->LOGGING2/2\n";
		}
		else if(CurrentState == WAITING)
		{
			//std::cout << "PHC->WAITING1/2\n";
			RTOS::event e = wait( HitFlag );

			GameTimePoolMutex.wait();
			timeVar = GameTimePool.read();
			GameTimePoolMutex.signal();
			if(timeVar >= 0 && timeVar < _GAMETIME)
				CurrentState = LOGGING;
			//std::cout << "PHC->WAITING2/2\n";
		}
	}
}

void ProcessHitController::HealthPlaced()
{
	HealthPlacedFlag.set();
}

void ProcessHitController::Hit()
{
	HitFlag.set();
}

void ProcessHitController::WriteIRResult(IRHitEvent irhe)
{
	IRResultPool.write(irhe);
}

void ProcessHitController::WriteGameTime(short time)
{
	GameTimePoolMutex.wait();
	GameTimePool.write(time);
	GameTimePoolMutex.signal();
}
